package com.thd.springboot.framework.cloud.gateway.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Component

public class PrintPathFilter implements GlobalFilter, Ordered {

    public PrintPathFilter(){
        System.out.println("================================= print path filter ============================");
    }
    @Override
    public int getOrder() {
        return 0;
    }
    private Logger logger = LoggerFactory.getLogger(PrintPathFilter.class.getName());

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        System.out.println("路径：;" + exchange.getRequest().getURI());
        return chain.filter(exchange);
    }

//    /**
//     * 用于查看nginx发送请求的路径
//     * explain:过滤的具体操作
//     * @param servletRequest
//     * @param servletResponse
//     * @param filterChain
//     * @throws IOException
//     * @throws ServletException
//     */
////    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        logger.info("请求地址:{}" , request.getRequestURI());
//
//        filterChain.doFilter(servletRequest,servletResponse);
//
//        /*
//        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
//
//        // 请求url中包含/hello||/online,继续执行
//        if (request.getRequestURI().contains("/hello")
//                || request.getRequestURI().contains("/online") ){
//            // 交给下一个过滤器或servlet处理
//            filterChain.doFilter(servletRequest,servletResponse);
//        }else {
//            // 请求url不包含/hello||/online,重定向到/online接口
//            wrapper.sendRedirect("/entry/online");
//        }*/
//    }

}
